import Mock from 'mockjs'
import { param2Obj } from '../utils/url'

let List = []
for (var i = 0; i < 192; i++) {
  List.push({
    id: Mock.mock('@zip()'),
    title: '幼儿英语入门课程',
    fee: Mock.mock('@integer(99, 999)'),
    sale: Mock.mock('@integer(1, 1000)'),
    person: Mock.mock('@first'),
    date: Mock.mock('@date()'),
    type: Mock.mock('@integer(0, 1)'),
  })
}

export default {
  getCourseData: config => {
    let { search, page, pageSize } = param2Obj(config.url)

    // 搜索
    let newList = List.filter(item => {
      return item.id.indexOf(search) != -1 || item.person.indexOf(search) != -1
    })

    // 分页
    let newList2 = newList.filter((item, index) => {
      return (page - 1) * pageSize <= index && index < page * pageSize
    })
    return {
      code: 200,
      msg: '请求成功',
      data: newList2,
      total: newList.length,
    }
  },

  // 删除单条
  deleteMockData: config => {
		console.log("123123");

    let { id } = JSON.parse(config.body)
    let newList = List.filter(item => {
      return item.id != id
    })
    List = newList
    return {
      code: 200,
      msg: '删除成功',
      data: null
    }
  },

  // 批量删除
  batchDeleteData: config => {
    let { ids } = JSON.parse(config.body)
    List = List.filter(item => {
      return ids.indexOf(item.id) === -1
    })
    return {
      code: 200,
      msg: '批量删除成功',
      data: null
    }
  },

  // 编辑
  updateCourse: config => {
    let { id, title, fee, person, type } = JSON.parse(config.body)
    List.forEach(item => {
      if (item.id == id) {
        item.title = title
        item.fee = fee
        item.person = person
        item.type = type
      }
    })
    return {
      code: 200,
      msg: '修改成功',
      data: null
    }
  },
}